<?php

class TagtypeController extends GTL_Action {

    public function init() {
        parent::init();
        $this->lng_id = '1';
       
        /* Initialize action controller here */
    }

    public function indexAction() {
       
        $this->_helper->redirector('tagcategory', 'tagtype', 'default');
    }

    public function tagcategoryAction(){
       
        $errorMessage = Array();
        $successmessage = "";
        $where = "";
        $orderField = 'lang_id';
        $sort = 'asc';
        $categoryCount = array();
        /* Pagination Login */
        $itemsPerPageReviews = $this->config->PAGING_PER_PAGE;
        $currentPageReviews = 1;
        
        //PAGING_PER_PAGE

        if ($this->_getParam('page') && $this->_getParam('page') != "") {
            $currentPageReviews = $this->_getParam('page');
        }

        if ($this->_getParam('field') && $this->_getParam('field') != "") {
            $orderField = $this->_getParam('field');
        }

        if ($this->_getParam('sort') && $this->_getParam('sort') != "") {
            $sort = $this->_getParam('sort');
        }

        $offset = ($currentPageReviews - 1) * $itemsPerPageReviews;
        $categoryModel = new Model_Propertymaintagtype();
       
        $categoryData = $categoryModel->fetchallmaintag($where, $itemsPerPageReviews, $offset, $orderField, $sort);
        $categoryCount = $categoryModel->fetchallmaintag($where);
        
        $paginator = new GTL_Paginator();
        $paginator->setItemsPerPage($itemsPerPageReviews);
        
        $paginator->setItemsTotal(count($categoryCount));
       
        $paginator->setCurrentPage($currentPageReviews);
        $link = $this->view->url(array('controller' => 'tagtype', 'action' => 'tagcategory', 'page' => 'PAGENO'), 'default', false);
        $paginator->paginate($link);

        
        //$this->view->languages = $languages;
        $this->view->paginator = $paginator;
        $this->view->categoryData = $categoryData;
        $this->view->errorMessage = $errorMessage;
        $this->view->successmessage = $successmessage;
         

    }

    public function tagsubcategoryAction(){
       
        
        $errorMessage = Array();
        $successmessage = "";
        $where = "";
        $orderField = 'ln.lang_id';
        $sort = 'asc';
        $subcategoryCount = array();
        
        /* Pagination Login */
        $itemsPerPageReviews = $this->config->PAGING_PER_PAGE;
        $currentPageReviews = 1;
        
        //PAGING_PER_PAGE

        if ($this->_getParam('page') && $this->_getParam('page') != "") {
            $currentPageReviews = $this->_getParam('page');
        }

        if ($this->_getParam('field') && $this->_getParam('field') != "") {
            $orderField = $this->_getParam('field');
        }

        if ($this->_getParam('sort') && $this->_getParam('sort') != "") {
            $sort = $this->_getParam('sort');
        }

        $offset = ($currentPageReviews - 1) * $itemsPerPageReviews;
        $subcategoryModel = new Model_Propertysubtagtype();
       
        $subcategoryData = $subcategoryModel->fetchallsubtag($where, $itemsPerPageReviews, $offset, $orderField, $sort);
        $subcategoryCount = $subcategoryModel->fetchallsubtag($where);
        
        $paginator = new GTL_Paginator();
        $paginator->setItemsPerPage($itemsPerPageReviews);
        
        $paginator->setItemsTotal(count($subcategoryCount));
       
        $paginator->setCurrentPage($currentPageReviews);
        $link = $this->view->url(array('controller' => 'tagtype', 'action' => 'tagsubcategory', 'page' => 'PAGENO'), 'default', false);
        $paginator->paginate($link);

        $this->view->paginator = $paginator;
        $this->view->subcategoryData = $subcategoryData;
        $this->view->errorMessage = $errorMessage;
        $this->view->successmessage = $successmessage;
         

    }

    public function taglabelAction(){
       
        
        $errorMessage = Array();
        $successmessage = "";
        $where = "";
        $orderField = 'prp_tgtype_id';
        $sort = 'asc';
        $labelCount = array();
        
        /* Pagination Login */
        $itemsPerPageReviews = $this->config->PAGING_PER_PAGE;
        $currentPageReviews = 1;
        
        //PAGING_PER_PAGE

        if ($this->_getParam('page') && $this->_getParam('page') != "") {
            $currentPageReviews = $this->_getParam('page');
        }

        if ($this->_getParam('field') && $this->_getParam('field') != "") {
            $orderField = $this->_getParam('field');
        }

        if ($this->_getParam('sort') && $this->_getParam('sort') != "") {
            $sort = $this->_getParam('sort');
        }

        $offset = ($currentPageReviews - 1) * $itemsPerPageReviews;
        $labelModel = new Model_Propertytagtype();
       
        $labelData = $labelModel->fetchalltag($where, $itemsPerPageReviews, $offset, $orderField, $sort);
        $labelCount = $labelModel->fetchalltag($where);
        
        $paginator = new GTL_Paginator();
        $paginator->setItemsPerPage($itemsPerPageReviews);
        
        $paginator->setItemsTotal(count($labelCount));
       
        $paginator->setCurrentPage($currentPageReviews);
        $link = $this->view->url(array('controller' => 'tagtype', 'action' => 'taglabel', 'page' => 'PAGENO'), 'default', false);
        $paginator->paginate($link);

        $this->view->paginator = $paginator;
        $this->view->labelData = $labelData;
        $this->view->errorMessage = $errorMessage;
        $this->view->successmessage = $successmessage;
         

    }

    public function tagcategoryformAction() {
        $successmessage = "";
        $languageDocuments = "";
        $mem_description = "";
        $errorMessageArray = Array();
        $this->view->mode = "Add";
        $data = Array();
         
        $categoryModel = new Model_Propertymaintagtype();
        $languageModel = new Model_Language();
        
        $categoryData = Array();
       // $categoryData['prp_typename_id'] = $this->_request->getParam('prp_typename_id');
       // $prp_typename_id = (!empty($categoryData['prp_typename_id'])) ? $categoryData['prp_typename_id'] : null;
        
        $categoryData['prp_typename_ref_id'] = $this->_request->getParam('prp_typename_ref_id');
        $prp_typename_ref_id = (!empty($categoryData['prp_typename_ref_id'])) ? $categoryData['prp_typename_ref_id'] : null;
        
        //$languageArray = $languageModel->fetchlanguageArray();
       // $categoryForm->prp_typename_lang_id->addMultiOptions($languageArray);
        
        $languagesArray = $languageModel->fetchlanguages();
        $languages = Array();
        foreach ($languagesArray as $key => $_lan) {
            $languages[$_lan['lang_id']] = $_lan;
            $languages[$_lan['lang_id']] = $_lan;
            $languages[$_lan['lang_id']]['prp_typename_name'] = "";
            $languages[$_lan['lang_id']]['prp_typename_ref_id'] = "";
            $languages[$_lan['lang_id']]['prp_typename_id'] = "";
           // $languages[$_lan['lang_id']]['prp_type_publish'] = "null";
        }
           
        if (!empty($prp_typename_ref_id)) {
            //Set the Mode to Edit
            $this->view->mode = "Edit";
            $ALLData = $categoryModel->fetchEntryByREFId($prp_typename_ref_id);
            foreach ($ALLData as $_desc) {
                if (array_key_exists($_desc['prp_typename_lang_id'], $languages)) {
                    $languages[$_desc['prp_typename_lang_id']]['prp_typename_name'] = $_desc['prp_typename_name'];
                    $languages[$_desc['prp_typename_lang_id']]['prp_typename_ref_id'] = $_desc['prp_typename_ref_id'];
                    $languages[$_desc['prp_typename_lang_id']]['prp_typename_id'] = $_desc['prp_typename_id'];
                  //  $languages[$_desc['prp_typename_lang_id']]['prp_type_publish'] = $_desc['prp_type_publish'];
                }
            }
        }

        if ($this->getRequest()->isPost()) {
            //Check for Errors
            $postedData = $this->_request->getPost();
                if($postedData['1_prp_typename_name']== ''){
                    $errorMessageArray[] = 'English value should not be blank.';
                    foreach ($languages as $key => $_lang) {
                        $languages[$key]['prp_typename_name'] = $postedData[$_lang['lang_id'].'_prp_typename_name'];
                        //$languages[$key]['prp_type_publish'] = $postedData[$_lang['lang_id'].'_prp_type_publish'];
                    }
                }
                if (count($errorMessageArray) > 0) {
                }else {
                    if($this->view->mode == 'Add'){
                        foreach ($languages as $key => $_lang) {
                            if($_lang['lang_id'] == 1){
                                $categoryData['prp_typename_name'] = $postedData[$_lang['lang_id'].'_prp_typename_name'];
                                $categoryData['prp_typename_lang_id'] = $_lang['lang_id'];
                                $categoryData['prp_type_publish'] = 1;
                                $categoryData['prp_typename_ref_id'] = $_lang['prp_typename_ref_id'];
                                $last_id = $categoryModel->save($categoryData, 'prp_typename_id');
                                $categoryData1['prp_typename_id'] = $last_id;
                                $categoryData1['prp_typename_ref_id'] = $last_id;
                                $categoryModel->save($categoryData1, 'prp_typename_id');
                            }else{
                                $categoryData['prp_typename_name'] = $postedData[$_lang['lang_id'].'_prp_typename_name'];
                                $categoryData['prp_typename_lang_id'] = $_lang['lang_id'];
                                $categoryData['prp_type_publish'] = 1;
                                $categoryData['prp_typename_ref_id'] = $last_id;
                                if($categoryData['prp_typename_name']!= '' ){
                                    $categoryModel->save($categoryData, 'prp_typename_id');
                                }
                            }
                        }
                   }
                    else{
                       //edit mode
                       foreach ($languages as $key => $_lang) {
                            if($_lang['lang_id'] == 1){
                                $categoryData['prp_typename_id'] = $_lang['prp_typename_id'];
                                $categoryData['prp_typename_name'] = $postedData[$_lang['lang_id'].'_prp_typename_name'];
                                $categoryData['prp_typename_lang_id'] = $_lang['lang_id'];
                                $categoryData['prp_type_publish'] = 1;
                                $categoryData['prp_typename_ref_id'] = $prp_typename_ref_id;
                                $categoryModel->save($categoryData, 'prp_typename_id');
                                
                            }else{
                                $categoryData['prp_typename_id'] = $_lang['prp_typename_id'];
                                $categoryData['prp_typename_name'] = $postedData[$_lang['lang_id'].'_prp_typename_name'];
                                $categoryData['prp_typename_lang_id'] = $_lang['lang_id'];
                                $categoryData['prp_type_publish'] = 1;
                                $categoryData['prp_typename_ref_id'] = $prp_typename_ref_id;
                                $categoryModel->save($categoryData, 'prp_typename_id');
                                
                            }
                        }
                     
                   }
                   if (count($errorMessageArray) > 0) {
                   }else{
                        if ($this->view->mode == "Add") {
                             $successmessage = 'Category add successfully';
                         } else {
                             $successmessage = 'Category updated successfully';
                         }
                         $this->addSuccessMessage($successmessage);
                         $this->_helper->redirector('tagcategory', 'tagtype', '');
                   }
                }
          
         }
        $this->view->languages = $languages;
        $this->view->errorMessageArray = $errorMessageArray;
        $this->view->succesMessage = $successmessage;
        $this->view->mode = $this->view->mode;
        
    }
    
    public function tagcategorydeleteAction() {
        $categoryModel = new Model_Propertymaintagtype();
        if ($this->_getParam('prp_typename_ref_id') && $this->_getParam('prp_typename_ref_id') != "") {
            $prp_typename_ref_id = $this->_getParam('prp_typename_ref_id');
            $categoryData = Array();
            $categoryData['prp_typename_ref_id'] = $prp_typename_ref_id;
            $categoryData['prp_type_publish'] = '2';
            $categoryModel->save($categoryData, 'prp_typename_ref_id');
            $successmessage = "Category deleted successfully";
            $this->addSuccessMessage($successmessage);
            $this->_helper->redirector('tagcategory', 'tagtype', '');
            exit;
        }
    }
    
    
    public function tagsubcategoryformAction() {
        $successmessage = "";
        $languageDocuments = "";
        $mem_description = "";
        $errorMessageArray = Array();
        $this->view->mode = "Add";
        $data = Array();
         
        $subcategoryModel = new Model_Propertysubtagtype();
        $categoryModel = new Model_Propertymaintagtype();
       // $subcategoryForm = new Form_TagsubcategoryForm();
        $languageModel = new Model_Language();
        
        $subcategoryData = Array();
        $subcategoryData['prp_subtag_ref_id'] = $this->_request->getParam('prp_subtag_ref_id');
        $prp_subtag_ref_id = (!empty($subcategoryData['prp_subtag_ref_id'])) ? $subcategoryData['prp_subtag_ref_id'] : null;
  
        
        //$languageArray = $languageModel->fetchlanguageArray();
        //$subcategoryForm->prp_sub_tag_lang_id->addMultiOptions($languageArray);
        
        
        $languagesArray = $languageModel->fetchlanguages();
        $languages = Array();
        foreach ($languagesArray as $key => $_lan) {
            $languages[$_lan['lang_id']] = $_lan;
            $languages[$_lan['lang_id']] = $_lan;
            $languages[$_lan['lang_id']]['prp_sub_tag_name'] = "";
            //$languages[$_lan['lang_id']]['prp_main_tag_id'] = "";
            $languages[$_lan['lang_id']]['prp_sub_tag_id'] = "";
            $languages[$_lan['lang_id']]['prp_subtag_ref_id'] = "";
        }
        
        $categoryArray = $categoryModel->fetchcategoryArray("prp_typename_lang_id =".$this->lng_id."");
        //$subcategoryForm->prp_main_tag_id->addMultiOptions($categoryArray);
        
        if (!empty($prp_subtag_ref_id)) {
            //Set the Mode to Edit
            $this->view->mode = "Edit";
            $data = $subcategoryModel->fetchEntryByREFId($prp_subtag_ref_id);
            $this->view->categoryid = $data[0]['prp_main_tag_id'];
            foreach ($data as $_desc) {
                if (array_key_exists($_desc['prp_sub_tag_lang_id'], $languages)) {
                    $languages[$_desc['prp_sub_tag_lang_id']]['prp_sub_tag_name'] = $_desc['prp_sub_tag_name'];
                   // $languages[$_desc['prp_sub_tag_lang_id']]['prp_main_tag_id'] = $_desc['prp_main_tag_id'];
                    $languages[$_desc['prp_sub_tag_lang_id']]['prp_sub_tag_id'] = $_desc['prp_sub_tag_id'];
                    $languages[$_desc['prp_sub_tag_lang_id']]['prp_subtag_ref_id'] = $_desc['prp_subtag_ref_id'];
                    
                }
            }
            
        }
        if ($this->getRequest()->isPost()) {
              //Check for Errors
            
                $postedData = $this->_request->getPost();
              
                if($postedData['prp_main_tag_id']== '' ){
                    $errorMessageArray[] = 'Please select category.';
                    $this->view->categoryid = $postedData['prp_main_tag_id'];
                }
                if($postedData['1_prp_sub_tag_name']== ''){
                        $errorMessageArray[] = 'English value should not be blank.';
                        foreach ($languages as $key => $_lang) {
                            $languages[$key]['prp_sub_tag_name'] = $postedData[$_lang['lang_id'].'_prp_sub_tag_name'];
                            //$languages[$key]['prp_main_tag_id'] = ['prp_main_tag_id'];
                        }
                         $this->view->categoryid = $postedData['prp_main_tag_id'];
                }
               
                if (count($errorMessageArray) > 0) {
                    
                }else {
                           /* $subcategoryData['prp_main_tag_id'] = $postedData['prp_main_tag_id'];
                            $subcategoryData['prp_sub_tag_name'] = $postedData['prp_sub_tag_name'];
                            $subcategoryData['prp_sub_tag_lang_id'] = $postedData['prp_sub_tag_lang_id'];
                            $subcategoryData['prp_sub_tag_publish'] = 1;
                            $subcategoryModel->save($subcategoryData, 'prp_sub_tag_id');
                            */
                     if($this->view->mode == 'Add'){
                        foreach ($languages as $key => $_lang) {
                            if($_lang['lang_id'] == 1){
                                $subcategoryData['prp_main_tag_id'] = $postedData['prp_main_tag_id'];
                                $subcategoryData['prp_sub_tag_name'] = $postedData[$_lang['lang_id'].'_prp_sub_tag_name'];
                                $subcategoryData['prp_sub_tag_lang_id'] = $_lang['lang_id'];
                                $subcategoryData['prp_sub_tag_publish'] = 1;
                                $subcategoryData['prp_subtag_ref_id'] = $_lang['prp_subtag_ref_id'];
                                $last_id = $subcategoryModel->save($subcategoryData, 'prp_sub_tag_id');
                                $subcategoryData1['prp_sub_tag_id'] = $last_id;
                                $subcategoryData1['prp_subtag_ref_id'] = $last_id;
                                $subcategoryModel->save($subcategoryData1, 'prp_sub_tag_id');
                            }else{
                                $subcategoryData['prp_main_tag_id'] = $postedData['prp_main_tag_id'];
                                $subcategoryData['prp_sub_tag_name'] = $postedData[$_lang['lang_id'].'_prp_sub_tag_name'];
                                $subcategoryData['prp_sub_tag_lang_id'] = $_lang['lang_id'];
                                $subcategoryData['prp_sub_tag_publish'] = 1;
                                $subcategoryData['prp_subtag_ref_id'] = $last_id;
                                if($subcategoryData['prp_sub_tag_name']!= '' ){
                                    $subcategoryModel->save($subcategoryData, 'prp_sub_tag_id');
                                }
                            }
                        }
                   }
                     else{
                       //edit mode
                       foreach ($languages as $key => $_lang) {
                            if($_lang['lang_id'] == 1){
                                $subcategoryData['prp_sub_tag_id'] = $_lang['prp_sub_tag_id'];
                                $subcategoryData['prp_main_tag_id'] = $postedData['prp_main_tag_id'];
                                $subcategoryData['prp_sub_tag_name'] = $postedData[$_lang['lang_id'].'_prp_sub_tag_name'];
                                $subcategoryData['prp_sub_tag_lang_id'] = $_lang['lang_id'];
                                $subcategoryData['prp_sub_tag_publish'] = 1;
                                $subcategoryData['prp_subtag_ref_id'] = $prp_subtag_ref_id;
                                $subcategoryModel->save($subcategoryData, 'prp_sub_tag_id');
                                
                            }else{
                                $subcategoryData['prp_sub_tag_id'] = $_lang['prp_sub_tag_id'];
                                $subcategoryData['prp_main_tag_id'] = $postedData['prp_main_tag_id'];
                                $subcategoryData['prp_sub_tag_name'] = $postedData[$_lang['lang_id'].'_prp_sub_tag_name'];
                                $subcategoryData['prp_sub_tag_lang_id'] = $_lang['lang_id'];
                                $subcategoryData['prp_sub_tag_publish'] = 1;
                                $subcategoryData['prp_subtag_ref_id'] = $prp_subtag_ref_id;
                                $subcategoryModel->save($subcategoryData, 'prp_sub_tag_id');
                                
                            }
                        }
                     
                   }
                   
                   
                              if (count($errorMessageArray) > 0) {

                              }else {
                                if ($this->view->mode == "Add") {
                                    $successmessage = 'Subcategory add successfully';
                                } else {
                                    $successmessage = 'Subcategory updated successfully';
                                }
                                $this->addSuccessMessage($successmessage);
                                $this->_helper->redirector('tagsubcategory', 'tagtype', '');
                              }
                }
           
         }
        $this->view->categoryArray = $categoryArray;
        $this->view->languages = $languages;
        $this->view->errorMessageArray = $errorMessageArray;
        $this->view->succesMessage = $successmessage;
        $this->view->mode = $this->view->mode;
        //$this->view->form = $subcategoryForm;
        
    }
    
    public function tagsubcategorydeleteAction() {
        $subcategoryModel = new Model_Propertysubtagtype();
        if ($this->_getParam('prp_subtag_ref_id') && $this->_getParam('prp_subtag_ref_id') != "") {
            $prp_subtag_ref_id = $this->_getParam('prp_subtag_ref_id');
            $subcategoryData = Array();
            $subcategoryData['prp_subtag_ref_id'] = $prp_subtag_ref_id;
            $subcategoryData['prp_sub_tag_publish'] = '2';
            $subcategoryModel->save($subcategoryData, 'prp_subtag_ref_id');
            $successmessage = "Subcategory deleted successfully";
            $this->addSuccessMessage($successmessage);
            $this->_helper->redirector('tagsubcategory', 'tagtype', '');
            exit;
        }
    }
    
    public function taglabelformAction() {
        $successmessage = "";
        $languageDocuments = "";
        $subcategoryArray = Array();
        $errorMessageArray = Array();
        $this->view->mode = "Add";
        $where = "";
        $data = Array();
         
        $subcategoryModel = new Model_Propertysubtagtype();
        $categoryModel = new Model_Propertymaintagtype();
        
        $labelModel = new Model_Propertytagtype();
      //  $labelForm = new Form_TaglabelForm();
        $languageModel = new Model_Language();
        
        $labelData = Array();
        $labelData['prp_tgtype_ref_id'] = $this->_request->getParam('prp_tgtype_ref_id');
        $prp_tgtype_ref_id = (!empty($labelData['prp_tgtype_ref_id'])) ? $labelData['prp_tgtype_ref_id'] : null;
  
        
        /*$languageArray = $languageModel->fetchlanguageArray();
        $labelForm->prp_tgtype_lang_id->addMultiOptions($languageArray);
        
        $categoryArray = $categoryModel->fetchcategoryArray();
        $labelForm->prp_tgtype_type->addMultiOptions($categoryArray);*/
        $languagesArray = $languageModel->fetchlanguages();
        $languages = Array();
        $this->view->subcategoryArray = /*$subcategoryArray = */$subcategoryModel->fetchsubcategoryArray();
        foreach ($languagesArray as $key => $_lan) {
            $languages[$_lan['lang_id']] = $_lan;
            $languages[$_lan['lang_id']] = $_lan;
            $languages[$_lan['lang_id']]['prp_tgtype_title'] = "";
            $languages[$_lan['lang_id']]['prp_tgtype_sub_tag'] = "";
            $languages[$_lan['lang_id']]['prp_tgtype_type'] = "";
            $languages[$_lan['lang_id']]['prp_tgtype_ref_id'] = "";
            $languages[$_lan['lang_id']]['prp_tgtype_id'] = "";
           // $languages[$_lan['lang_id']]['sub_cat'] = $subcategoryArray;
        }
        
        $categoryArray = $categoryModel->fetchcategoryArray("prp_typename_lang_id =".$this->lng_id."");
        
        
       
       /* $labelForm->prp_tgtype_sub_tag->addMultiOptions($subcategoryArray);*/
        
        if (!empty($prp_tgtype_ref_id)) {
            //Set the Mode to Edit
            $this->view->mode = "Edit";
            //Fetch data to be loaded in form 
            $data = $labelModel->fetchEntryByREFId($prp_tgtype_ref_id);
            $this->view->categoryid = $data[0]['prp_tgtype_type'];
            $this->view->subcategoryid = $data[0]['prp_tgtype_sub_tag'];
            foreach ($data as $_desc) {
                if($_desc['prp_tgtype_type']!= ''){
                    $add_condition = "and prp_main_tag_id = ".$_desc['prp_tgtype_type'];
                }else{
                    $add_condition = "";
                }
                
                $subcategoryArray =  $subcategoryModel->fetchsubcategoryArray("prp_sub_tag_lang_id =".$this->lng_id." ".$add_condition);
                if (array_key_exists($_desc['prp_tgtype_lang_id'], $languages)) {
                      $languages[$_desc['prp_tgtype_lang_id']]['prp_tgtype_title'] = $_desc['prp_tgtype_title'];
                      $languages[$_desc['prp_tgtype_lang_id']]['prp_tgtype_sub_tag'] = $_desc['prp_tgtype_sub_tag'];
                      $languages[$_desc['prp_tgtype_lang_id']]['prp_tgtype_type'] = $_desc['prp_tgtype_type'];
                      $languages[$_desc['prp_tgtype_lang_id']]['prp_tgtype_ref_id'] = $_desc['prp_tgtype_ref_id'];
                      $languages[$_desc['prp_tgtype_lang_id']]['prp_tgtype_id'] = $_desc['prp_tgtype_id'];
                     // $languages[$_desc['prp_tgtype_lang_id']]['sub_cat'] = $subcategoryArray;
                }
            }
            $this->view->subcategoryArray = $subcategoryArray ;
           //echo 'in';exit;
        }
        
        if ($this->getRequest()->isPost()) {
              //Check for Errors
            $postedData = $this->_request->getPost();
           
                if($postedData['prp_tgtype_type'] == ''){
                    $errorMessageArray[] = 'Please select category';
                }
                if($postedData['prp_tgtype_sub_tag']== ''){
                    $errorMessageArray[] = 'Please select subcategory';
                }
                if($postedData['1_prp_tgtype_title']== ''){
                        $errorMessageArray[] = 'English value should not be blank.';
                        foreach ($languages as $key => $_lang) {
                            if ($postedData['prp_tgtype_type'] != '') {
                                $add_condition = "and prp_main_tag_id = " . $postedData['prp_tgtype_type'];
                            } else {
                                $add_condition = "";
                            }
                            $subcategoryArray = $subcategoryModel->fetchsubcategoryArray("prp_sub_tag_lang_id =".$this->lng_id." ".$add_condition);                 
                            $languages[$key]['prp_tgtype_sub_tag'] = $postedData[$_lang['lang_id'].'_prp_tgtype_sub_tag'];
                            $languages[$key]['prp_tgtype_type'] = $postedData[$_lang['lang_id'].'_prp_tgtype_type'];
                            $languages[$key]['prp_tgtype_title'] = $postedData[$_lang['lang_id'].'_prp_tgtype_title'];
                        }
                    }
                    $this->view->subcategoryArray = $subcategoryArray;
                    if(array_key_exists('prp_tgtype_type', $postedData)){
                        $this->view->categoryid = $postedData['prp_tgtype_type'];
                    }
                    if(array_key_exists('prp_tgtype_sub_tag', $postedData)){
                        $this->view->subcategoryid = $postedData['prp_tgtype_sub_tag'];
                    }
                    
                    
            
           
                if (count($errorMessageArray) > 0) {
                    
                }else {
                           /* $labelData['prp_tgtype_title'] = $postedData['prp_tgtype_title'];
                            $labelData['prp_tgtype_sub_tag'] = $postedData['prp_tgtype_sub_tag'];
                            $labelData['prp_tgtype_type'] = $postedData['prp_tgtype_type'];
                            $labelData['prp_tgtype_lang_id'] = $postedData['prp_tgtype_lang_id'];
                            $labelData['prp_tgtype_publish'] = 1;
                            $labelModel->save($labelData, 'prp_tgtype_id');
                            * 
                            */
                     if($this->view->mode == 'Add'){
                        foreach ($languages as $key => $_lang) {
                            if($_lang['lang_id'] == 1){
                                $labelData['prp_tgtype_title'] = $postedData[$_lang['lang_id'].'_prp_tgtype_title'];
                                $labelData['prp_tgtype_sub_tag'] = $postedData['prp_tgtype_sub_tag'];
                                $labelData['prp_tgtype_type'] = $postedData['prp_tgtype_type'];
                                $labelData['prp_tgtype_lang_id'] = $_lang['lang_id'];
                                $labelData['prp_tgtype_publish'] = 1;
                                $labelData['prp_tgtype_ref_id'] = $_lang['prp_tgtype_ref_id'];
                                $last_id = $labelModel->save($labelData, 'prp_tgtype_id');
                                $labelData1['prp_tgtype_id'] = $last_id;
                                $labelData1['prp_tgtype_ref_id'] = $last_id;
                                $labelModel->save($labelData1, 'prp_tgtype_id');
                            }else{
                                $labelData['prp_tgtype_title'] = $postedData[$_lang['lang_id'].'_prp_tgtype_title'];
                                $labelData['prp_tgtype_sub_tag'] = $postedData['prp_tgtype_sub_tag'];
                                $labelData['prp_tgtype_type'] = $postedData['prp_tgtype_type'];
                                $labelData['prp_tgtype_lang_id'] = $_lang['lang_id'];
                                $labelData['prp_tgtype_publish'] = 1;
                                $labelData['prp_tgtype_ref_id'] = $last_id;
                                if($labelData['prp_tgtype_title']!= '' ){
                                   $labelModel->save($labelData, 'prp_tgtype_id');
                                }
                            }
                        }
                   }
                     else{
                       //edit mode
                       foreach ($languages as $key => $_lang) {
                            if($_lang['lang_id'] == 1){
                                $labelData['prp_tgtype_id'] = $_lang['prp_tgtype_id'];
                                $labelData['prp_tgtype_title'] = $postedData[$_lang['lang_id'].'_prp_tgtype_title'];
                                $labelData['prp_tgtype_sub_tag'] = $postedData['prp_tgtype_sub_tag'];
                                $labelData['prp_tgtype_type'] = $postedData['prp_tgtype_type'];
                                $labelData['prp_tgtype_lang_id'] = $_lang['lang_id'];
                                $labelData['prp_tgtype_publish'] = 1;
                                $labelData['prp_tgtype_ref_id'] = $prp_tgtype_ref_id;
                                $labelModel->save($labelData, 'prp_tgtype_id');
                                
                            }else{
                                $labelData['prp_tgtype_id'] = $_lang['prp_tgtype_id'];
                                $labelData['prp_tgtype_title'] = $postedData[$_lang['lang_id'].'_prp_tgtype_title'];
                                $labelData['prp_tgtype_sub_tag'] = $postedData['prp_tgtype_sub_tag'];
                                $labelData['prp_tgtype_type'] = $postedData['prp_tgtype_type'];
                                $labelData['prp_tgtype_lang_id'] = $_lang['lang_id'];
                                $labelData['prp_tgtype_publish'] = 1;
                                $labelData['prp_tgtype_ref_id'] = $prp_tgtype_ref_id;
                                $labelModel->save($labelData, 'prp_tgtype_id');
                                
                            }
                        }
                     
                   }
                              if (count($errorMessageArray) > 0) {

                              }else {
                                if ($this->view->mode == "Add") {
                                    $successmessage = 'Label add successfully';
                                } else {
                                    $successmessage = 'Label updated successfully';
                                }
                                $this->addSuccessMessage($successmessage);
                                $this->_helper->redirector('taglabel', 'tagtype', '');
                              }
                }
           
         }
        
        $this->view->subcategoryArray = $subcategoryArray;
        $this->view->categoryArray = $categoryArray;
        $this->view->languages = $languages;
        $this->view->errorMessageArray = $errorMessageArray;
        $this->view->succesMessage = $successmessage;
        $this->view->mode = $this->view->mode;
        //$this->view->form = $labelForm;
        
    }
    
    public function taglabeldeleteAction() {
        $labelModel = new Model_Propertytagtype();
        if ($this->_getParam('prp_tgtype_ref_id') && $this->_getParam('prp_tgtype_ref_id') != "") {
            $prp_tgtype_ref_id = $this->_getParam('prp_tgtype_ref_id');
            $labelData = Array();
            $labelData['prp_tgtype_ref_id'] = $prp_tgtype_ref_id;
            $labelData['prp_tgtype_publish'] = '2';
            $labelModel->save($labelData, 'prp_tgtype_ref_id');
            $successmessage = "Label deleted successfully";
            $this->addSuccessMessage($successmessage);
            $this->_helper->redirector('taglabel', 'tagtype', '');
            exit;
        }
    }
    
    
    public function getsubcategoryAction(){
        $String = "";
        if ($this->getRequest()->isPost()) {
            $postedData = $this->_request->getPost();
            if ($postedData['category_id'] != '') {

               $subcategoryModel = new Model_Propertysubtagtype();
               $where = "prp_sub_tag_lang_id =".$this->lng_id." and prp_main_tag_id = ".$postedData['category_id'];
               $subcategoryArray = $subcategoryModel->fetchsubcategoryArray($where);

                if (count($subcategoryArray) > 0) {
                    $String = "";
                    foreach ($subcategoryArray as $key => $value) {
                        $String .= "<option value='" . $key . "'>" . $value . "</option>";
                    }
                }
            }
        }
        echo $String;
        exit;
    }
  
}

   
?>
